package com.campus.web.controller;


import com.alibaba.fastjson.JSON;
import com.campus.app.mapper.Update;
import com.campus.app.service.UsersService;
import com.campus.commons.entity.User;
import com.campus.commons.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UsersService usersService;
    @GetMapping("/all")
    public String getAllUser() {
        return JSON.toJSONString(usersService.getOne(null,false));
    }

    @PostMapping("/get")
    @ApiOperation("获取用户信息")
    public Result getUserById(Integer id) {
        return usersService.getUserById(id);
    }

    @PostMapping("/gPhone")
    @ApiOperation("验证手机号是否存在")
    public Result isExistsPhone(@NotBlank@Length(max = 11,min = 11,message = "请输入正确的手机号") String phone) {
        return usersService.isExistsByPhone(phone);
    }

    @PostMapping("/upPwd")
    @ApiOperation("修改用户密码")
    public Result updatePassword(@NotBlank@Length(max = 11,min = 11,message = "请输入正确的手机号")String phone, Integer id,@NotBlank@Length(min = 6,max = 30,message = "密码长度过短安全性过低") String password) {
        return Result.result(usersService.UpdateUser(new User(id,password,phone)));
    }
    @ApiOperation("更新用户信息")
    @PostMapping("/update")
    public Result updateInfo(@Validated(Update.class) User user) {
        return Result.result(usersService.updateById(user));
    }
}

